<?php

Loader::using("models::entities::enums::TiposEmpresas");

/**
 * Description of CrearSolicitud
 *
 * @export Modules
 * export-metadata RequiredRoles:ROLE_PRACTICES_COORDINATOR
 */
class GestionEmpresas extends Module {

    public function index() {

        $Empresas = $this->model->companies->GetAll();

        $this->assign("Empresas", $Empresas);
        $this->assign("action", $this->route->action("AgregarEmpresa"));
        $this->assign("action2", $this->route->action("EditarEmpresa"));
        $this->assign("action3", $this->route->moduleAction("ReportarAmonestacion", "index"));
        $this->assign("action4", $this->route->action("Eliminar"));

        $this->display();
    }

    public function AgregarEmpresa() {

        $Sectores = Sector::values();
        $TiposEmpresas = TiposEmpresas::values();

        $this->assign("TiposEmpresas", $TiposEmpresas);
        $this->assign("Sectores", $Sectores);
        $this->assign("UrlVolver", $this->route->action("index"));
        $this->assign("action", $this->route->action("GuardarEmpresas"));
        $this->assign("paises", $this->model->countries->GetAll(false));

        $this->display();
    }

    Public function AgregarEmpresaGenerico() {
        $Sectores = Sector::values();
        $TiposEmpresas = TiposEmpresas::values();

        $this->assign("TiposEmpresas", $TiposEmpresas);
        $this->assign("Sectores", $Sectores);    
        $this->assign("action", $this->route->action("GuardarEmpresas"));
        $this->assign("paises", $this->model->countries->GetAll(false));

        $this->display();
    }

    public function EditarEmpresa() {

        $Sectores = Sector::values();
        $TiposEmpresas = TiposEmpresas::values();


        $this->assign("TiposEmpresas", $TiposEmpresas);
        $this->assign("Sectores", $Sectores);
        $this->assign("UrlVolver", $this->route->action("index"));
        $this->assign("Paises", $this->model->countries->GetAll(FALSE));
        $this->assign("empresa", $this->model->companies->GetById($this->request->get("Nit")));
        $this->assign("Sectores", $Sectores);
        $this->assign("action", $this->route->action("GuardarEmpresas"));
        $this->assign("paises", $this->model->countries->GetAll(false));

        $this->assign("EditarEmpresa", $this->route->action("GuardarEditarEmpresa"));
        $this->display();
    }

    public function GuardarEditarEmpresa() {
        if ($this->request->submitted()) {

            Loader::using("models::entities::Empresas");


            $Empresa = new Empresas();
            $Empresa->setDatosContacto(new DatosContacto());
            
            $this->assign("UrlVolver", $this->route->action("index"));
            $this->request->bind($Empresa);

            IF ($Empresa->getTieneConvenio() == "false") {
                $Empresa->setVigencia(NULL);
            }

            $this->model->companies->Update($Empresa);

            $this->addMessage(new Message("", "Datos Actualizados con Exito !!!", Message::SUCCESS));

            $this->display();
        } else {
            $this->index();
        }
    }

    public function GuardarEmpresas() {

        if ($this->request->submitted()) {

            Loader::using("models::entities::Empresas");

            $Empresa = new Empresas();
            $Empresa->setDatosContacto(new DatosContacto());

            $this->request->bind($Empresa);
            $this->assign("UrlVolver", $this->route->action("index"));      
            
            IF ($Empresa->getTieneConvenio() == "false") {
                $Empresa->setVigencia(NULL);
            }

            $this->model->companies->Create($Empresa);

            $this->addMessage(new Message("", "Empresa Creada Exitosamente !!!", Message::SUCCESS));

            $this->display();
        } else {
            $this->index();
        }
    }
    public function Eliminar() {
        Loader::using("models::entities::Empresas");
        
        $Nit=$this->request->get("Nit");
        
        $this->assign("Empresa", $this->model->companies->Delete($Nit));
        
        $this->execute("index");
    }

}

?>
